const moment = date => {
	/**
	 * 处理判断数据
	 * @param  {Date}  startDate                                比较日期，必须
	 * @param  {Date}  endDate                                  比较日期，必须 
	 * @param  {String}  unit                                   比较单位，必须(默认全部比较)
	 * @param  {String}  type                                   比较类型，必须
	 */
	const dealAndJudgeData = (startDate, endDate, unit, type) => {
		let $s = '',
			$e = '',
			s = '',
			e = '',
			res = false;
		switch (unit) {
			case 'year':
				s = startDate.split('-')[0]
				e = endDate.split('-')[0]
				break;
			case 'month':
				s = startDate.substring(0, startDate.lastIndexOf('-'))
				e = endDate.substring(0, endDate.lastIndexOf('-'))
				break;
			default:
				s = startDate
				e = endDate
				break;
		}
		switch (type) {
			case 'after':
				$s = new Date(Date.parse(s));
				$e = new Date(Date.parse(e));
				res = $s > $e
				break;
			case 'before':
				$s = new Date(Date.parse(s));
				$e = new Date(Date.parse(e));
				res = $s < $e
				break;
			case 'same':
				res = s === e
				break;
		}
		return res
	}
	const addZeroPrefix = number => {
		return number < 10 ? `0${number}` : number
	}
	const $moment = {
		isAfter: (afterDate, unit) => {
			return dealAndJudgeData(date, afterDate, unit, 'after')
		},
		isBefore: (beforeDate, unit) => {
			return dealAndJudgeData(date, beforeDate, unit, 'before')
		},
		isSame: (sameDate, unit) => {
			return dealAndJudgeData(date, sameDate, unit, 'same')
		},
		isContinuity: (date1, date2) => {
			let d1 = new Date(date1),
				d2 = new Date(date2),
				days = d2.getTime() - d1.getTime(),
				res = parseInt(days / (1000 * 60 * 60 * 24));
			return res > 1 // 大于一为连续日期
		},
		getSomeDate: (value, unit, splitor = '-') => {
			if (!value) return date;

			let $d = +new Date(date),
				lw = new Date($d + 1000 * 60 * 60 * 24 * value), //value天数
				y = lw.getFullYear(),
				M = lw.getMonth() + 1,
				d = lw.getDate();

			return `${ y }${ splitor }${ addZeroPrefix(M) }${ splitor }${ addZeroPrefix(d) }`;
		},
		getDateSpan: value => {
			if (!value) return;

			return Math.floor((Math.abs(Date.parse(value) - Date.parse(date))) / (24 * 3600 * 1000));
		},
		getTimePeriodPrefix: () => {
			let prefix = '',
				h = date.getHours()
			if (h >= 6 && h <= 8) {
				prefix = '早上'
			} else if (h >= 8 && h <= 11) {
				prefix = '上午'
			} else if (h >= 11 && h <= 14) {
				prefix = '中午'
			} else if (h >= 14 && h <= 18) {
				prefix = '下午'
			} else if (h >= 18 && h <= 20) {
				prefix = '傍晚'
			} else if (h >= 20 && h <= 0) {
				prefix = '晚上'
			} else if (h >= 0 && h <= 6) {
				prefix = '凌晨'
			}

			return prefix
		},
		isToday: () => {
			return date.toDateString() === new Date().toDateString()
		},
		format: (form, splitor = '-') => {
			let y = date.getFullYear(),
				M = date.getMonth() + 1,
				d = date.getDate(),
				h = date.getHours(),
				m = date.getMinutes(),
				s = date.getSeconds(),
				res = '';

			switch (form) {
				case 'YYYY':
					res = `${ y }`
					break;
				case 'MM':
					res = `${ addZeroPrefix(M) }`
					break;
				case 'DD':
					res = `${ addZeroPrefix(d) }`
					break;
				case 'HH':
					res = `${ addZeroPrefix(h) }`
					break;
				case 'mm':
					res = `${ addZeroPrefix(m) }`
					break;
				case 'SS':
					res = `${ addZeroPrefix(s) }`
					break;
				case 'MM-DD':
					res = `${ addZeroPrefix(M) }${ splitor }${ addZeroPrefix(d) }`
					break;
				case 'HH:mm:ss':
					res = `${ addZeroPrefix(h) }:${ addZeroPrefix(m) }:${ addZeroPrefix(s) }`
					break;
				case 'mm:ss':
					res = `${ addZeroPrefix(m) }:${ addZeroPrefix(s) }`
					break;
				case 'HH:mm':
					res = `${ addZeroPrefix(h) }:${ addZeroPrefix(m) }`
					break;
				case 'YYYY-MM':
					res = `${ y }${ splitor }${ addZeroPrefix(M) }`
					break;
				case 'YYYY-MM-DD':
					res = `${ y }${ splitor }${ addZeroPrefix(M) }${ splitor }${ addZeroPrefix(d) }`
					break;
				case 'YYYY-MM-DD HH':
					res = `${ y }${ splitor }${ addZeroPrefix(M) }${ splitor }${ addZeroPrefix(d) } ${ addZeroPrefix(h) }`
					break;
				case 'YYYY-MM-DD HH:mm':
					res =
						`${ y }${ splitor }${ addZeroPrefix(M) }${ splitor }${ addZeroPrefix(d) } ${ addZeroPrefix(h) }:${ addZeroPrefix(m) }`
					break;
				case 'YYYY-MM-DD HH:mm:ss':
					res =
						`${ y }${ splitor }${ addZeroPrefix(M) }${ splitor }${ addZeroPrefix(d) }${ addZeroPrefix(h) }:${ addZeroPrefix(m) }:${ addZeroPrefix(s) }`
					break;
			}
			return res
		}
	}
	return $moment
}
export default moment
